package net.galacticwar.replayserver.admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import net.galacticwar.replayserver.ServerManagerMBean;

public class PropertyController {

	private final LoginCallback loginCallback;
	private final ServerManagerMBean mbeanProxy;
	
	private PropertyFrame frame;
	
	public PropertyController(LoginCallback loginCallback, ServerManagerMBean mbeanProxy) {
		this.loginCallback = loginCallback;
		this.mbeanProxy = mbeanProxy;
		
		frame = new PropertyFrame();
		
		
		update();
		initActions();
		
	
	}
	
	private void update(){
		frame.getFilterCheckBox().setSelected(mbeanProxy.isReplayFilterActive());
		
		((DefaultListModel)frame.getReplayList().getModel()).clear();
		String[] replays = mbeanProxy.getReplays();
		for (String name : replays) {
			((DefaultListModel)frame.getReplayList().getModel()).addElement(name);
		}
		
		String[] allowednames = mbeanProxy.getRegisteredPlayers();
		((DefaultListModel)frame.getNameList().getModel()).clear();
		for (String name : allowednames) {
			((DefaultListModel)frame.getNameList().getModel()).addElement(name);
		}


	}
	
	private void initActions() {
		frame.getQuitButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				loginCallback.disconnect();
			}
		});

		frame.getCloseServerButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				int val = JOptionPane.showConfirmDialog(frame, "Are you sure to close the server ?", "Server shutdown confirmation", JOptionPane.YES_NO_OPTION);
				if (val == JOptionPane.YES_OPTION) {
					mbeanProxy.closeServer(true);
				}
			}
		});
		
		frame.getRefreshButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				update();
			}
		});

		frame.getReplayListenersButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				Object o = frame.getReplayList().getSelectedValue();
				if (o == null) {
					JOptionPane.showMessageDialog(frame, "Please select a replay in the list");
					return;
				}
				
				String[] replays = mbeanProxy.getReplayListeners((String) o);
				StringBuilder sb = new StringBuilder();
				sb.append("There is ");
				sb.append(replays.length);
				sb.append(" listeners.\r\n");
				
				int i= 0;
				for (String string : replays) {
					sb.append(string);
					sb.append(", ");
					i++;
					if (i % 5 == 0) {
						sb.append("\r\n");
					}
				}
				
				JOptionPane.showMessageDialog(frame, sb.toString());
			}
		});
		
		frame.getFilterCheckBox().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				mbeanProxy.setReplayFilterActive(frame.getFilterCheckBox().isSelected());
			}
		});

		frame.getLoadReplayButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				String name = JOptionPane.showInputDialog(frame, "Enter the replay file name");
				if (name == null || name.length()==0) {
					return;
				}
				String result = mbeanProxy.loadReplay(name);
				JOptionPane.showMessageDialog(frame, result);
			}
		});
		
		frame.getCloseReplayButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				Object o = frame.getReplayList().getSelectedValue();
				if (o == null) {
					JOptionPane.showMessageDialog(frame, "Please select a replay in the list");
					return;
				}

				String result = mbeanProxy.closeReplay((String) o, false);
				update();
				JOptionPane.showMessageDialog(frame, result);
				
			}
		});
		
		
		frame.getReplayDetailsButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				Object o = frame.getReplayList().getSelectedValue();
				if (o == null) {
					JOptionPane.showMessageDialog(frame, "Please select a replay in the list");
					return;
				}

				String result = mbeanProxy.getReplayDetails((String) o);
				JOptionPane.showMessageDialog(frame, result);
			}
		});
		
		frame.getAddPlayerButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				String name = JOptionPane.showInputDialog(frame, "Enter the name of the player");
				if (name == null || name.length()==0) {
					return;
				}
				String result = mbeanProxy.registerPlayer(name);
				update();
			}
		});

		frame.getRemovePlayerButton().addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				Object o = frame.getNameList().getSelectedValue();
				if (o == null) {
					JOptionPane.showMessageDialog(frame, "Please select a name in the list");
					return;
				}
				String result = mbeanProxy.unregisterPlayer((String) o);
				update();
			}
		});

	}
	
	
	public void show(){
		frame.setVisible(true);
	}
	
	
}
